package com.et.machine.manager.vm.goods

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.recyclerview.widget.DiffUtil
import com.et.machine.convert.machineIdInfo
import com.et.machine.convert.machineInfo
import com.et.machine.manager.vm.goods.AddGoodsFragment.Companion.BOX_01
import com.et.machine.manager.vm.goods.AddGoodsFragment.Companion.BOX_02
import com.et.machine.manager.vm.goods.AddGoodsFragment.Companion.BOX_03
import com.et.machine.manager.vm.goods.AddGoodsFragment.Companion.BOX_04
import com.et.machine.manager.vm.goods.AddGoodsFragment.Companion.BOX_05
import com.et.machine.manager.vm.goods.AddGoodsFragment.Companion.BOX_06
import com.et.machine.manager.vm.goods.AddGoodsFragment.Companion.BOX_07
import com.et.machine.manager.vm.goods.AddGoodsFragment.Companion.BOX_08
import com.et.machine.manager.vm.goods.AddGoodsFragment.Companion.BOX_09
import com.et.machine.manager.vm.goods.AddGoodsFragment.Companion.BOX_11
import com.et.machine.manager.vm.goods.AddGoodsFragment.Companion.Box
import com.et.machine.store.ChannelEntity
import com.et.machine.util.LabelChannel
import kotlin.math.ceil
import kotlin.math.max

@Suppress("NAME_SHADOWING")
class AddGoodsViewModel : ViewModel() {

    private val foodNumber =
        (machineInfo?.nFood!!) +
                (machineIdInfo?.nFood2!!) +
                (machineIdInfo?.nFood3!!) +
                (machineIdInfo?.nFood4!!)


    /**
     * box box count
     */
    fun addBoxList(): MutableList<Box> {
        val boxData = mutableListOf<Box>()
        Log.d(
            "MachineIdInfo", "nFood:${machineInfo?.nFood!!}, " +
                    "nFood2:${(machineIdInfo?.nFood2!!)}, " +
                    "nFood3:${(machineIdInfo?.nFood3!!)}, " +
                    "nFood4:${(machineIdInfo?.nFood4!!)}"
        )

        if (machineInfo?.cDrink!! > 0) {
            boxData.add(Box(false, BOX_11))
        }
        for (i in 9 downTo 10 - ceil(foodNumber / 48.0).toInt()) {
            boxData.add(Box(false, i))
        }
        for (i in 1..(machineInfo?.cBox!!)) {
            boxData.add(Box(false, i))
        }

        return boxData
    }

    /**
     * channel Label
     */
    fun getChannelLabel(box: Int, channel: Int): String {
        val ins = LabelChannel.getInstance()
        ins.apply {
            initFoodChannelCount(9, machineInfo?.nFood ?: 0)
            initFoodChannelCount(8, machineIdInfo?.nFood2 ?: 0)
            initFoodChannelCount(7, machineIdInfo?.nFood3 ?: 0)
            initFoodChannelCount(6, machineIdInfo?.nFood4 ?: 0)
        }
        return ins.channelLabel(box, channel)
    }


    fun box2Name(id: Int): String {
        return when (id) {
            BOX_11 -> "$id 饮料机"
            BOX_06, BOX_07, BOX_08, BOX_09 -> "$id 食品机"
            BOX_01, BOX_02, BOX_03, BOX_04, BOX_05 -> "$id 格子柜"
            else -> "unknown"
        }
    }

    /**
     * 物理料道号转变逻辑料道号
     * 只针对食品机器
     *
     * @param maxNum9 9食品机的最大料道数
     * @param maxNum8 8食品机的最大料道数
     * @param maxNum7 7食品机的最大料道数
     * @param maxNum6 6食品机的最大料道数
     * @param box     1 - 11
     * @param num     0 - 47
     * @return
     */
    private fun transRoad(
        maxNum9: Int,
        maxNum8: Int,
        maxNum7: Int,
        maxNum6: Int,
        box: Int,
        num: Int
    ): String? {
        var maxNum7 = maxNum7
        if (maxNum9 == 60 && maxNum8 == 60) {
            maxNum7 = 60
        }
        val sum = maxNum9 + maxNum8 + maxNum7 + maxNum6
        val aisle = arrayOfNulls<String>(sum)
        val xuhao = arrayOfNulls<String>(sum)
        for (i in 0 until sum) {
            xuhao[i] = "" + (9 - i / 48) + String.format("%02d", (i % 48 + 1))
        }
        // 48/56/60/70/80
        for (i in 0 until maxNum9) {
            val row = if (maxNum9 % 10 == 0) 10 else 8
            var x = i / row
            var y = i % row
            x++
            if (row != 10) {
                y++
            }
            aisle[i] = "9$x$y"
        }
        for (i in 0 until maxNum8) {
            val row = if (maxNum8 % 10 == 0) 10 else 8
            var x = i / row
            var y = i % row
            x++
            if (row != 10) {
                y++
            }
            aisle[maxNum9 + i] = "8$x$y"
        }
        for (i in 0 until maxNum7) {
            val row = if (maxNum7 % 10 == 0) 10 else 8
            var x = i / row
            var y = i % row
            x++
            if (row != 10) {
                y++
            }
            aisle[maxNum9 + maxNum8 + i] = "7$x$y"
        }
        for (i in 0 until maxNum6) {
            val row = if (maxNum6 % 10 == 0) 10 else 8
            var x = i / row
            var y = i % row
            x++
            if (row != 10) {
                y++
            }
            aisle[maxNum9 + maxNum8 + maxNum7 + i] = "6$x$y"
        }
        for (i in 0 until sum) {
            if (xuhao[i] == "" + box + String.format("%02d", num)) {
                return aisle[i]
            }
        }
        return ""
    }

    private fun channelCount(box: Int): Int {

        return when (box) {
            11 -> {
                machineInfo?.nDrink!!
            }
            9, 8, 7, 6 -> {
                max(48.coerceAtMost(foodNumber - (9 - box) * 48), 0)
            }
            1, 2, 3, 4, 5 -> {
                40
            }
            else -> 0
        }
    }
    /**
     * 获取传入GoodInfo的货柜号及货道号字符串  return 饮料机只有逻辑货到01-48 没有11柜号
     * ， 格子柜 1 2  < 6 返回 101 102
     * ， 食品机 9 8 7 6 返回 911 912
     * ， 饮料机 11 返回 01 02 不带11 因为只有11不要表示
     *
     * @param channelType
     * @param channelNum
     * @return
     */
    fun getChannelInfo(
        channelType: Int,
        channelNum: Int
    ): String {
        return if (channelType == GoodsFragment.BOX_11) {
            String.format("%02d", channelNum)
        } else if (channelType == GoodsFragment.BOX_01 || channelType == GoodsFragment.BOX_02 || channelType == GoodsFragment.BOX_03 || channelType == GoodsFragment.BOX_04 || channelType == GoodsFragment.BOX_05) {
            "" + channelType + String.format("%02d", channelNum)
        } else {
            transRoad(
                channelCount(GoodsFragment.BOX_09),
                channelCount(GoodsFragment.BOX_08),
                channelCount(GoodsFragment.BOX_07),
                channelCount(GoodsFragment.BOX_06),
                channelType,
                channelNum
            )
        } ?: ""
    }

}

class DiffCallBack(
    private val src: MutableList<ChannelEntity>,
    private val dest: MutableList<ChannelEntity>
) :
    DiffUtil.Callback() {
    override fun getOldListSize(): Int = src.size

    override fun getNewListSize(): Int = dest.size

    override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        return true
    }


    override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        return false
    }
}